﻿
using NavPlus;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 表现层的导航单元
/// </summary>
public class NavGridView : CustomUIInteraction 
{
    NavGridLogic m_NavGridLogic;
    int gridWidth = 100;

    // UI 组件
    public Image stateImage;
    public Text indexText;
    public Text disText;
    public Transform arrowRoot;

    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="data"></param>
    public void InitNavGridView(NavGridLogic data)
    {
        m_NavGridLogic = data;
        name = $"grid_{m_NavGridLogic.XPos}_{m_NavGridLogic.YPos}";
        RectTransform rect = GetComponent<RectTransform>();
        rect.localScale = Vector3.one;
        rect.localPosition = new Vector3(m_NavGridLogic.XPos * gridWidth, m_NavGridLogic.YPos * gridWidth);
        indexText.text = $"{m_NavGridLogic.XPos},{m_NavGridLogic.YPos}";

        m_NavGridLogic.OnViewChange = (state) =>
        {
            SetView(state);
        };

        OnClickDown = () =>
        {
            if (Input.GetMouseButton(0))
            {
                if (m_NavGridLogic.IsWalkable)
                {
                    Launcher.Instance.OnClickGridUnit(m_NavGridLogic);
                }
                else
                {
                    Debug.Log("click grid is unwalkable.");
                }
            }
            else if (Input.GetMouseButton(1)) 
            {
                StateEnum currState = Launcher.Instance.currState;
                if (currState == StateEnum.Prepare)
                {
                    m_NavGridLogic.SetWalkableState(!m_NavGridLogic.IsWalkable);
                }
                else
                {
                    Debug.LogWarning($"current state:{currState},please reset map data.");
                }
            }
        };
        OnEnter = () =>
        {
            if (Input.GetMouseButton(1))
            {
                StateEnum currState = Launcher.Instance.currState;
                if (currState == StateEnum.Prepare)
                {
                    m_NavGridLogic.SetWalkableState(!m_NavGridLogic.IsWalkable);
                }
                else
                {
                    Debug.LogWarning($"current state:{currState},please reset map data.");
                }
            }
        };  
    }

    /// <summary>
    /// 反初始化
    /// </summary>
    public void UnInitNavGridView()
    {
        SetView(ViewState.Walk);
    }

    void SetView(ViewState state)
    {
        switch (state)
        {
            case ViewState.Walk:
                stateImage.color = Color.white;
                disText.gameObject.SetActive(false);
                arrowRoot.gameObject.SetActive(false);
                break;
            case ViewState.Block:
                stateImage.color = Color.black;
                disText.gameObject.SetActive(false);
                arrowRoot.gameObject.SetActive(false);
                break;
            case ViewState.StartPoint:
                stateImage.color = Color.blue;
                break;
            case ViewState.EndPoint:
                stateImage.color = Color.red;
                break;
            case ViewState.CheckedPoint:
                stateImage.color = Color.magenta;
                arrowRoot.gameObject.SetActive(false);
                disText.gameObject.SetActive(true);
                disText.text = m_NavGridLogic.SumDistance.ToString();
                break;
            case ViewState.DebugDir:
                ShowSearchDirection(Color.yellow);
                disText.text = m_NavGridLogic.SumDistance.ToString();
                break;
            case ViewState.PathResult:
                ShowSearchDirection(Color.green);
                disText.text = m_NavGridLogic.SumDistance.ToString();
                break;
        }

        void ShowSearchDirection(Color color)
        {
            if (m_NavGridLogic.PreGrid != null)
            {
                stateImage.color = color;
                arrowRoot.gameObject.SetActive(true);
                int deltX = m_NavGridLogic.XPos - m_NavGridLogic.PreGrid.XPos;
                int deltY = m_NavGridLogic.YPos - m_NavGridLogic.PreGrid.YPos;
                float angle = Vector3.SignedAngle(Vector3.right, new Vector3(deltX, deltY, 0), Vector3.forward);
                arrowRoot.transform.localEulerAngles = new Vector3(0, 0, angle);
            }
            else
            {
                arrowRoot.gameObject.SetActive(false);
            }
        }
    }
}

public enum ViewState
{
    Walk,
    Block,
    StartPoint,
    EndPoint,
    CheckedPoint,
    DebugDir,
    PathResult,
}

